package com.zzyl.controller;


import com.aliyun.iot20180120.models.*;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.entity.Device;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/iot")
@Api(tags = "设备管理 - 物联网")
public class DeviceController {

    @Autowired
    DeviceService deviceService;

    @PostMapping("syncProductList")
    @ApiOperation(value = "同步产品列表")
    public ResponseResult syncProductList() {
        deviceService.syncProductList();
        return ResponseResult.success();
    }

    //    接口地址:/iot/allProduct
//    请求方式:GET
//    请求参数:
    @GetMapping("allProduct")
    @ApiOperation(value = "获取产品列表")
    public ResponseResult<List<ProductVo>> allProduct() {
        List<ProductVo> list = deviceService.allProduct();
        return ResponseResult.success(list);
    }

    /**
     * 注册设备
     *
     * @param deviceDto
     * @return
     */
    @PostMapping("RegisterDevice")
    @ApiOperation(value = "注册设备")
    public ResponseResult registerDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.registerDevice(deviceDto);
        return ResponseResult.success();
    }


    /**
     * 分页查询设备
     *
     * @param pageNum
     * @param pageSize
     * @param deviceName
     * @param locationType
     * @param productKey
     * @return
     */
    @GetMapping("pageQueryDevice")
    @ApiOperation(value = "分页查询设备")
    public ResponseResult<PageResponse<DeviceVo>> pageQueryDevice(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                                  @RequestParam(value = "deviceName", required = false) String deviceName,
                                                                  @RequestParam(value = "locationType", required = false) Integer locationType,
                                                                  @RequestParam(value = "productKey", required = false) String productKey) {
        PageResponse<DeviceVo> pageResponse = deviceService.pageQueryDevice(pageNum, pageSize, deviceName, locationType, productKey);
        return ResponseResult.success(pageResponse);
    }


    /**
     * 查询设备详情
     * @param
     * @return
     */
    @PostMapping("QueryDeviceDetail")
    @ApiOperation(value = "查询设备详情")
    public ResponseResult<DeviceVo> QueryDeviceDetail(@RequestBody QueryDeviceDetailRequest request) {
        DeviceVo deviceVo = deviceService.getDeviceDetail(request);
        return ResponseResult.success(deviceVo);
    }


    /**
     * 查询指定设备的物模型运行状态
     * @param
     * @return
     */
    @PostMapping("QueryDevicePropertyStatus")
    @ApiOperation(value = "查询指定设备的物模型运行状态")
    public ResponseResult queryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest request) {
        return deviceService.queryDevicePropertyStatus(request);
    }

    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     * @param request
     * @return
     */
    @PostMapping("/QueryThingModelPublished")
    @ApiOperation(value = "查看指定产品的已发布物模型中的功能定义详情")
    public ResponseResult queryThingModelPublished(@RequestBody QueryThingModelPublishedRequest request) {
        return deviceService.queryThingModelPublished(request);
    }

    /**
     * 更新设备
     * @param deviceDto
     * @return
     */
    @PostMapping("UpdateDevice")
    @ApiOperation(value = "更新设备")
    public ResponseResult UpdateDevice(@RequestBody DeviceDto deviceDto) {
        deviceService.UpdateDevice(deviceDto);
        return ResponseResult.success();
    }


}
